package com.maltys.dp.backpack;

import java.util.Arrays;

/**
 * dp[i][j] = dp[i-1][j]
 * dp[i][j-i*i]+1
 *
 * @author malty
 * @link <a href="https://leetcode.cn/problems/perfect-squares/description/">完全平方数</a>
 */
public class PerfectSquares {
    public static void main(String[] args) {
        PerfectSquares subSet = new PerfectSquares();
        int canPartition = subSet.numSquares(1);
        System.out.println(canPartition);
    }

    public int numSquares(int n) {
        int sqrt = (int) Math.sqrt(n + 0.0);
        int[][] dp = new int[sqrt + 1][n + 1];
        for (int[] ints : dp) {
            Arrays.fill(ints, Integer.MAX_VALUE >> 1);
        }
        dp[0][0] = 0;
        for (int i = 1; i <= sqrt; i++) {
            for (int j = 0; j <= n; j++) {
                dp[i][j] = dp[i - 1][j];
                if (j >= i * i) {
                    dp[i][j] = Math.min(dp[i][j], dp[i][j - i * i] + 1);
                }
            }
        }
        return dp[sqrt][n];
    }
}
